<template>
  <view class="content" v-if="pagestate">
    <view class="paytype">
      <image
        v-if="orderDetail.status == 1"
        class="paytype_icon"
        src="../../static/Pending_payment.png"
      ></image>
      <image
        v-else-if="orderDetail.status == 2"
        class="paytype_icon"
        src="../../static/Pending_payment.png"
      ></image>
      <image
        v-else-if="orderDetail.status == 3"
        class="paytype_icon"
        src="../../static/Paid.png"
      ></image>
      <image
        v-else-if="orderDetail.status == 4"
        class="paytype_icon"
        src="../../static/unpaid.png"
      ></image>

      <view v-if="orderDetail.status == 1" class="paytype_status">上架中</view>
      <view v-else-if="orderDetail.status == 2" class="paytype_status"
        >等待买家付款</view
      >
      <view v-else-if="orderDetail.status == 3" class="paytype_status"
        >交易成功</view
      >
      <view v-else-if="orderDetail.status == 4" class="paytype_status"
        >超时未支付</view
      >

      <view
        v-if="orderDetail.status == 1"
        class="paytype_detail paytype_highlight"
        >等待匹配买家</view
      >

      <view
        v-if="orderDetail.status == 2"
        class="paytype_detail paytype_highlight"
      >
        <view
          class=""
          style="display: flex; align-content: center; justify-content: center"
        >
          <u-count-down
            :timestamp="oldTime"
            separator="colon"
            separator-color="#1FE7E7"
            separator-size="28"
            font-size="28"
            @end="end()"
            color="#1FE7E7"
            :show-border="false"
            bg-color="transparent"
          ></u-count-down>
          <view class="unpaid_left">后关闭</view>
        </view>
      </view>
      <view v-else-if="orderDetail.status == 3" class="paytype_detail"
        >可在我的藏品看到该藏品</view
      >
      <view v-else-if="orderDetail.status == 4" class="paytype_detail"
        >订单已取消</view
      >
    </view>

    <view class="record" v-if="orderDetail_product">
      <image class="record_image" :src="orderDetail_product.cover"></image>

      <view class="record_content">
        <view class="record_content_title">{{ orderDetail_product.name }}</view>
        <view>
          <view class="record_content_no">NO:{{ orderDetail_product.no }}</view>
        </view>
        <view class="record_content_third">
          <view class="record_content_price"
            >¥ {{ orderDetail_product.price }}</view
          >
        </view>
      </view>
    </view>

    <view class="detail" v-if="orderDetail.status != 1">
      <view class="detail_row">
        <view class="detail_row_left">卖家</view>
        <view class="record_content_info">
          <image
            class="record_content_avatar"
            :src="orderDetail_product.creator_avatar"
          ></image>
          <view class="detail_row_right">{{
            orderDetail_product.creator_account
          }}</view>
        </view>
      </view>

      <view class="detail_row">
        <view class="detail_row_left">买家</view>
        <view class="record_content_info">
          <image
            class="record_content_avatar"
            :src="orderDetail_product.user_avatar"
          ></image>
          <view class="detail_row_right">{{
            orderDetail_product.user_account
          }}</view>
        </view>
      </view>

      <view class="detail_row">
        <view class="detail_row_left">订单金额</view>
        <view class="detail_row_right">{{ orderDetail_product.price }}元</view>
      </view>

      <view class="detail_row">
        <view class="detail_row_left">平台手续费</view>
        <view class="detail_row_right"
          >{{ orderDetail.consignment_last_order.commission_money }}元</view
        >
      </view>

      <view class="detail_row">
        <view class="detail_row_left">挂售商家手续费</view>
        <view class="detail_row_right"
          >{{
            orderDetail.consignment_last_order.linked_merchant_commission_money
          }}元</view
        >
      </view>

      <!-- 微信，支付宝 -->
      <!-- <view class="detail_row" v-if="orderDetail.status == 2">
        <view class="detail_row_left">支付方式</view>
        <view class="detail_row_right">
          <text v-if="orderDetail.pay_type == 1">余额</text>
          <text v-else-if="orderDetail.pay_type == 2">支付宝</text>
          <text
            v-else-if="orderDetail.pay_type == 3 || orderDetail.pay_type == 4"
            >微信</text
          >
        </view>
      </view> -->

      <view class="detail_row">
        <view class="detail_row_left">交易数量</view>
        <view class="detail_row_right">1</view>
      </view>

      <view class="detail_row">
        <view class="detail_row_left">创建时间</view>
        <view class="detail_row_right">{{
          orderDetail.consignment_last_order.created_at
        }}</view>
      </view>

      <view class="detail_row">
        <view class="detail_row_left">订单编号</view>
        <view class="detail_row_right">{{
          orderDetail.consignment_last_order.order_no
        }}</view>
      </view>

      <view class="detail_row" v-if="orderDetail.status == 4">
        <view class="detail_row_left">取消时间</view>
        <view class="detail_row_right">{{ orderDetail.expire_at }}</view>
      </view>

      <view class="detail_row" v-if="orderDetail.status == 3">
        <view class="detail_row_left">支付时间</view>
        <view class="detail_row_right">{{
          orderDetail.consignment_last_order.pay_at
        }}</view>
      </view>

      <view class="detail_row">
        <view class="detail_row_left">上架时间</view>
        <view class="detail_row_right">{{ orderDetail.put_on_at }}</view>
      </view>
    </view>

    <view class="bottom" v-if="orderDetail.status == 1">
      <view class="bottom_button" @click="$u.debounce(offline, 1000, true)"
        >立即下架</view
      >
    </view>
  </view>
</template>


<script>
import api from "@/api/api.js";
import tip from "@/utils/tip.js";
import { isLogin } from "@/utils/auth.js";
import { getTimeleft } from "@/utils/timeleft.js";
import moment from "moment";

export default {
  data() {
    return {
      show: false,
      payTypeChecked: 2,
      id: "",
      orderDetail: [],
      orderDetail_product: [],
      IPaddress: "",
      oldTime: "",
      index: null,
      pagestate: false,
    };
  },
  onLoad(options) {
    this.id = options.id;
    this.index = options.index;

    let windowElement = uni.getSystemInfoSync();
    this.widHeigh = windowElement.windowHeight;

    this.IPaddress = uni.getStorageSync("ipAddress");
  },
  onShow() {
    this.getOrderDetail();
  },
  methods: {
    end() {
      this.getOrderDetail();
    },
    handleCheckPayType(type) {
      this.payTypeChecked = type;
    },
    handlePayImmediately() {
      this.offline();
    },
    async getOrderDetail() {
      // this.orderDetail.status = 1

      let res = await api.releaseDetail({
        method: "POST",
        query: {
          consignment_id: this.id,
        },
        loading: "加载中",
      });
      if (res.code === 0) {
        console.log(res.data, "rews");
        this.orderDetail = res.data;
        this.pagestate = true;
        console.log(this.orderDetail, "this.orderDetail");

        // if (this.orderDetail.status == 1) {
        this.orderDetail_product = {
          cover: res.data.user_product.product.cover,
          name: res.data.user_product.product.name,
          creator_avatar:
            this.orderDetail.status != 1
              ? res.data.user_product.user.avatar
              : "",
          creator_account:
            this.orderDetail.status != 1
              ? res.data.user_product.user.account
              : "",
          no: res.data.user_product.no,
          price: res.data.price,
          user_avatar:
            this.orderDetail.status != 1
              ? res.data.consignment_last_order.user.avatar
              : "",
          user_account:
            this.orderDetail.status != 1
              ? res.data.consignment_last_order.user.account
              : "",
        };
        if (this.orderDetail.status == 2) {
          let newTime = moment();
          this.oldTime =
            getTimeleft(
              this.orderDetail.consignment_last_order.expire_at,
              newTime
            ).duration + 1;
        }

        // }
      } else {
        tip.defaults(res.msg);
      }
    },

    async offline() {
      console.log("1");
      let res = await api.unrelease({
        method: "post",
        query: {
          consignment_id: this.id,
        },
        loading: "加载中",
      });
      if (res.code == 0) {
        uni.$emit("offshop", { index: this.index });
        tip.defaults("下架成功");
        setTimeout(() => {
          uni.navigateBack({
            delta: 1, // 回退前 delta(默认为1) 页面
          });
        }, 500);
      }
    },
    async info() {
      let res = await api.info({});
      uni.setStorageSync("userInfo", res.data);
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../../style/yuanchuang.scss";
.content {
  font-family: PingFangSC-Regular, PingFang SC;
  padding: 30rpx 30rpx 100rpx 30rpx;
  background: $bg-color1;

  min-height: 100vh;
}
.paytype {
  margin: auto;
  text-align: center;
  &_icon {
    width: 80rpx;
    height: 80rpx;
  }
  &_status {
    margin-top: 20rpx;
    height: 50rpx;
    font-size: 36rpx;
    font-weight: 500;
    color: $text-color3;
    line-height: 50rpx;
  }
  &_detail {
    margin-top: 10rpx;
    height: 40rpx;
    font-size: 28rpx;
    font-weight: 400;
    line-height: 40rpx;
    color: #1fe7e7;
  }
  &_highlight {
    color: $text-color3;
  }
}
.record {
  margin-top: 40rpx;
  display: flex;
  align-items: center;
  width: 690rpx;
  height: 242rpx;
  border-radius: 24rpx;

  background-image: url(../../static/transaction_record_bg.png);
  background-repeat: no-repeat;
  background-size: 100% 242rpx;
  &_image {
    width: 242rpx;
    height: 242rpx;
    border-radius: 24rpx;
  }
  &_content {
    flex: 1;
    padding: 30rpx;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    height: 242rpx;
    &_no {
      font-size: 14px;
      color: $text-color1;
    }
    &_title {
      height: 44rpx;
      font-size: 32rpx;
      font-weight: 500;
      color: $text-color1;
      line-height: 44rpx;

      overflow: hidden;
      -webkit-line-clamp: 1;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-box-orient: vertical;
    }
    &_second {
      margin-top: 20rpx;
      display: flex;
      align-items: center;
    }
    &_avatar {
      width: 36rpx;
      height: 36rpx;
      border-radius: 50%;
    }
    &_username {
      margin-left: 8rpx;
      height: 36rpx;
      font-size: 26rpx;
      font-weight: 500;
      color: $text-color1;
      line-height: 36rpx;
    }
    &_third {
      display: flex;
      align-items: center;
      justify-content: space-between;
      // margin-top: 40rpx;
    }
    &_price {
      height: 42rpx;
      font-size: 36rpx;
      font-family: SFPro-Bold, SFPro;
      font-weight: bold;
      color: $text-color1;
      line-height: 42rpx;
    }
    &_status {
      height: 34rpx;
      font-size: 24rpx;
      font-weight: 400;
      color: #39a2ff;
      line-height: 34rpx;
    }
  }
}
.detail {
  margin-top: 30rpx;
  padding: 30rpx;
  width: 690rpx;
  // height: 420rpx;
  background: $bg-color3;
  border-radius: 24rpx;
  &_row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .record_content_info {
      display: flex;
      align-content: center;
      image {
        margin: auto 10rpx;
      }
    }
    &_left {
      height: 40rpx;
      font-size: 28rpx;
      font-weight: 400;
      color: #bbbbbe;
      line-height: 40rpx;
    }
    &_right {
      height: 40rpx;
      font-size: 28rpx;
      font-weight: 400;
      color: $text-color1;
      line-height: 40rpx;
    }
  }
  &_row:not(:first-child) {
    margin-top: 40rpx;
  }
}
.bottom {
  padding: 24rpx 0;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 3;

  // width: 750rpx;
  height: 138rpx;
  &_button {
    margin: 0 auto;
    width: 650rpx;
    height: 90rpx;
    color: $text-color1;
    background: $button;
    background-repeat: no-repeat;
    // background-attachment: fixed;
    background-size: 100% 100%;
    // border-radius: 16rpx;

    font-size: 32rpx;
    font-weight: 400;
    line-height: 90rpx;
    text-align: center;
  }
}
.popup_box {
  //   width: 750rpx;
  width: 100%;
  height: 100%;
  padding: 0rpx 50rpx;
  background: #101428;
  margin: 30rpx 0 60rpx;
  border-radius: 24rpx;
  .popup_box_top {
    font-size: 28rpx;
    font-weight: 400;
    color: #0c1125;
    padding: 30rpx 0 10rpx;
  }
  &_title {
    height: 40rpx;
    font-size: 28rpx;
    font-weight: 400;
    color: #bbbbbe;
    line-height: 40rpx;
    text-align: center;
  }
  &_price {
    margin-top: 16rpx;
    height: 58rpx;
    font-size: 48rpx;
    font-weight: bold;
    color: #0c1125;
    line-height: 58rpx;
    text-align: center;
  }
  &_title2 {
    margin-top: 52rpx;
    height: 40rpx;
    font-size: 28rpx;
    font-weight: 400;
    color: #0c1125;
    line-height: 40rpx;
  }
  &_paytype {
    padding: 40rpx 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    &_left {
      display: flex;
      align-items: center;
    }
    &_icon {
      width: 40rpx;
      height: 40rpx;
    }
    &_type {
      margin-left: 20rpx;
      height: 44rpx;
      font-size: 32rpx;
      font-weight: 500;
      color: #0c1125;
      line-height: 44rpx;
    }
    &_checked {
      width: 40rpx;
      height: 40rpx;
    }
  }
  &_paytype + &_paytype {
    border-top: 2rpx solid #262934;
  }
  &_button {
    margin-top: 30rpx;
    height: 90rpx;
    background: linear-gradient(90deg, #39a2ff 0%, #b641fe 100%);
    border-radius: 16rpx;

    font-size: 32rpx;
    font-weight: 400;
    color: #0c1125;
    line-height: 90rpx;
    text-align: center;
  }
}
</style>